package com.hnkywl.sts.dao.zjgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.zjgl.Xyz;
import com.hnkywl.sts.entity.zjgl.Xyzfy;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.sfgl
 * 类描述:    付款审核dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class XyzfyDao extends SimpleHibernateTemplate<Xyzfy, Long> {

    public Page<Xyzfy> findAllXyzfy(String xyzhm,Long gsId,Integer xyzdrZt,Integer voucherNum,String czr,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Xyzfy> page = new Page<Xyzfy>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (xyzhm != null && !("").equals(xyzhm)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append("xyz.xyzhm like ? ");
            param.add("%"+xyzhm.trim()+"%");
        }
        if (gsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append("xyz.nbgs.id = ? ");
            param.add(gsId);
        }
        if (voucherNum != null && ! voucherNum.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append("xyz.voucherNum = ? ");
            param.add(voucherNum);
        }
        if (czr != null && !czr.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append("xyz.createUser.xm like ?");
            param.add("%" +czr.trim()+ "%");
        }
        if (xyzdrZt != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            //status=0未导入
            //status=1导入成功
            //status=2导入失败
            //status=3删除成功
            //status=4删除失败
            condition.append(" ("+xyzdrZt+"=0 and insertFlag=1) " +
                    "or ("+xyzdrZt+"=1 and insertFlag=3 and deleteFlag!=2) " +
                    "or ("+xyzdrZt+"=2 and insertFlag=2) " +
                    "or ("+xyzdrZt+"=3 and deleteFlag=3 and insertFlag!=2) " +
                    "or ("+xyzdrZt+"=4 and deleteFlag=2) " );
        }
        //==============记录数量===============
        hql = "select count(*) from Xyzfy  where 1=1 "+ RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Xyzfy  where 1=1 "  + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

//    public Page<Xyz> findxyzListByIds(String xyzIds,Integer  xyzsflxType,int pageNo, int pageSize) {
//        String hql = null;
//        List<Object> param = new ArrayList<Object>();
//        StringBuilder condition = new StringBuilder();
//        Page<Xyz> page = new Page<Xyz>(pageNo, pageSize, 0);
//        //==============条件拼接===============
//        if (xyzsflxType != null) {
//            if (condition.length() != 0)
//                condition.append(" and ");
//            condition.append(" xyzsflx = ? ");
//            param.add(xyzsflxType);
//        }
//        if (xyzIds != null) {
//            if (condition.length() != 0)
//                condition.append(" and ");
//            condition.append(" id in("+xyzIds+") ");
//        }
//        //==============记录数量===============
//        hql = "select count(*) from Xyz where 1=1" ;
//        if (condition.length() != 0)
//            hql = hql + " and " + condition ;
//        hql = hql +" order by dqrq desc";
//        Long c = null;
//        if (param.size() != 0) {
//            c = (Long) findUnique(hql, param.toArray());
//            page.setTotalCount(c.intValue());
//        } else {
//            c = (Long) findUnique(hql);
//            page.setTotalCount(c.intValue());
//        }
//        if (c == 0)
//            return page;
//        //==============查询结果===============
//        hql = "from Xyz where 1=1" ;
//        if (condition.length() != 0)
//            hql = hql + " and " + condition ;
//        hql = hql +" order by dqrq desc";
//        if (param.size() != 0)
//            return find(page, hql, param.toArray());
//        return find(page, hql);
//    }
//
//
//    /**
//     * 查询未使用的信用证
//     * @return
//     */
//    public Page<Xyz> findwsyXyzList(Integer  pjsflxType,int pageNo, int pageSize) {
//        String hql = null;
//        List<Object> param = new ArrayList<Object>();
//        StringBuilder condition = new StringBuilder();
//        Page<Xyz> page = new Page<Xyz>(pageNo, pageSize, 0);
//        //==============条件拼接===============
//        if (pjsflxType != null) {
//            if (condition.length() != 0)
//                condition.append(" and ");
//            condition.append(" xyzsflx = ? ");
//            param.add(pjsflxType);
//        }
//        //==============记录数量===============
//        hql = "select count(*) from Xyz where status = 1 " ;
//        if (condition.length() != 0)
//            hql = hql + " and " + condition ;
//        hql = hql +" order by dqrq desc";
//        Long c = null;
//        if (param.size() != 0) {
//            c = (Long) findUnique(hql, param.toArray());
//            page.setTotalCount(c.intValue());
//        } else {
//            c = (Long) findUnique(hql);
//            page.setTotalCount(c.intValue());
//        }
//        if (c == 0)
//            return page;
//        //==============查询结果===============
//        hql = "from Xyz where status = 1 "  ;
//        if (condition.length() != 0)
//            hql = hql + " and " + condition ;
//        hql = hql +" order by dqrq desc";
//        if (param.size() != 0)
//            return find(page, hql, param.toArray());
//        return find(page, hql);
//    }
//
//    /**
//     * 判断属性是否重复
//     * @param id 信用证id
//     * @param propertyName 付款审核属性名
//     * @param propertyValue 付款审核属性值
//     * @return
//     */
//    public Boolean checkUnique(Long id, String propertyName,Object propertyValue) {
//        String hql;
//        if (propertyValue == null)
//            return false;
//        if(id == null){
//            //修改检验
//            hql = "from Xyz where 1=1 and " + propertyValue + " = ? ";
//            return this.existsQuery(hql, propertyName);
//        }else{
//            //新增检验
//            hql = "from Xyz where 1 =1 and " + propertyValue + " = ? and id <>?";
//            return this.existsQuery(hql, propertyName,id);
//        }
//    }

    public Xyzfy findXyzfyByXyzId(Long xyzId){
        return (Xyzfy)find("from Xyzfy where xyz.id = ? ",xyzId).get(0);
    }


}
